$(document).ready(function () {
    // 注册
    $("#btn_register").click(function () {
        var fd = new FormData(document.getElementById('lycms-register'));
        $.ajax({
            url: '/v1/user/action/system/register',
            type: 'POST',
            timeout: 5000,
            dataType: 'json',
            contentType: 'application/x-www-form-urlencoded;charset=utf-8',
            data: fd,
            contentType: false, //不设置内容类型
            processData: false, //不处理数据
            success: function (data) {
                if (data.code == 200) {
                    layer.msg(data.msg, {
                        time: 1500
                    }, function () {
                        self.location.href = "/user/login.html"
                    });
                } else {
                    layer.msg(data.msg);
                    $('#captchaPic').click();
                }
            },
            error: function (XMLResponse) {
                $('#captchaPic').click();
            }
        });
    });

    // 登录
    $("#btn_login").click(function () {
        if ($('#user_name').val() == '') {
            layer.msg('请输入用户账号！');
            $("#user_name").focus();
            return false;
        }
        if ($('#user_pwd').val() == '') {
            layer.msg('请输入密码！');
            $("#user_pwd").focus();
            return false;
        }

        var fd = new FormData(document.getElementById('lycms-login'));
        var index = layer.load(0, {
            shade: false
        });
        $.ajax({
            url: '/v1/user/action/system/login',
            type: 'POST',
            timeout: 5000,
            dataType: 'json',
            contentType: 'application/x-www-form-urlencoded;charset=utf-8',
            data: fd,
            contentType: false, //不设置内容类型
            processData: false, //不处理数据
            success: function (data) {
                if (data.code == 200) {
                    layer.msg(data.msg, {
                        time: 1500
                    }, function () {
                        self.location.href = "/user/index.html"
                    });
                } else {
                    layer.msg(data.msg);
                    $('#captcha').click();
                }
                layer.close(index);
            },
            error: function (XMLResponse) {
                $('#captcha').click();
                layer.close(index);
            }
        });
    });

    // 获取验证码
    function getMeailCode(data) {
        $.ajax({
            url: "/v1/user/action/system/getCode",
            type: "post",
            dataType: "json",
            data: data,
            contentType: false, //不设置内容类型
            processData: false, //不处理数据
            success: function (r) {
                if (r.code == '200') {
                    layer.alert(r.msg, {
                        icon: 1,
                    });
                } else {
                    layer.alert(r.msg, {
                        icon: 0,
                    });
                }
            },
            error: function (XMLResponse) {
                layer.alert("获取验证码失败", {
                    icon: 2,
                });
            }
        });
    }

    // 验证码倒计时
    var countdown = 60;

    // 倒计时
    function settime(val) {
        if (countdown == 0) {
            val.removeAttribute("disabled");
            $('#getCode').css('background-color', '');
            val.value = "获取";
            countdown = 60;
            return true;
        } else {
            $('#getCode').css('background-color', '#d2d2d2');
            $('#getCode').attr("disabled", true);
            val.value = "等待(" + countdown + ")";
            countdown--;
        }
        setTimeout(function () {
            settime(val)
        }, 1000)
    }



    // 验证码获取
    $('#getCode').click(function () {
        var email = $('#email').val();
        var pattern = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        var ex = pattern.test(email);
        if (!ex) {
            layer.alert('邮箱格式不正确', {
                icon: 2,
            });
            return;
        }
        // 倒计时
        settime(this);
        var data = $("#fm").serialize();
        var fd = new FormData();
        fd.append('email', email);
        fd.append('type', "register");
        getMeailCode(fd);
    });
});